package ui.activity.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import base.BaseAct;
import butterknife.BindView;
import event.NetErrorEvent;
import ui.activity.login.biz.ForgetPwdBiz;
import ui.activity.login.component.DaggerForgetPwdComponent;
import ui.activity.login.contract.ForgetPwdContract;
import ui.activity.login.module.ForgetPwdModule;
import ui.activity.login.presenter.ForgetPwdPresenter;
import widget.ClearEditText;


/**
 * 忘记密码
 * Created by chenjian on 2017/8/4.
 */

public class ForgetPwdAct extends BaseAct implements ForgetPwdContract.View {


    @Inject
    ForgetPwdPresenter presenter;
    @Inject
    ForgetPwdBiz biz;

    @BindView(R.id.edit_forgetpass_phonenum)
    ClearEditText phone;
    @BindView(R.id.forgetpass_txt)
    TextView getCode;
    @BindView(R.id.edit_forgetpass_verificationcode)
    ClearEditText code;
    @BindView(R.id.forgetpass_linear1)
    LinearLayout linear1;
    @BindView(R.id.edit_forgetpass_newpass)
    ClearEditText newpass;
    @BindView(R.id.edit_forgetpass_confimpass)
    ClearEditText confimpass;
    @BindView(R.id.forgetpass_linear2)
    LinearLayout linear2;
    @BindView(R.id.but_next)
    Button next;
    @BindView(R.id.but_confim)
    Button confim;
    @BindView(R.id.forgetpassword_linear3)
    LinearLayout linear3;
    @BindView(R.id.forgetpassword_linear4)
    LinearLayout linear4;

    private CountDownTimer timer;


    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_forgetpassword);
    }

    @Override
    protected void setup() {
        DaggerForgetPwdComponent.builder().forgetPwdModule(new ForgetPwdModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.forgetpass), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        RxView.clicks(getCode)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.getPhoneCode(phone.getText().toString().trim()));
        RxView.clicks(next)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toNext(phone.getText().toString().trim(), code.getText().toString().trim()));
        RxView.clicks(confim)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.getPwdInfo(phone.getText().toString().trim(), newpass.getText().toString().trim(), confimpass.getText().toString().trim()));
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
        if (timer != null) {
            timer.cancel();
        }
    }


    @Override
    public void upDateUI() {
        if (timer != null) {
            timer.cancel();
        }
        getCode.setEnabled(false);
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                getCode.setText(l / 1000 + "秒后重发");
            }

            @Override
            public void onFinish() {
                getCode.setEnabled(true);
                getCode.setText("获取验证码");
            }
        };
        timer.start();
    }

    @Override
    public void upDateType() {
        linear1.setVisibility(View.GONE);
        linear2.setVisibility(View.VISIBLE);
        linear3.setVisibility(View.GONE);
        linear4.setVisibility(View.VISIBLE);
    }

}
